package com.studyweb.service.impl;

import com.studyweb.mapper.PaperMapper;
import com.studyweb.mapper.PaperQuestionMapper;
import com.studyweb.mapper.PoliticsQuestionMapper;
import com.studyweb.mapper.CommonSenseQuestionMapper;
import com.studyweb.mapper.LanguageQuestionMapper;
import com.studyweb.mapper.QuantitativeQuestionMapper;
import com.studyweb.mapper.JudgmentQuestionMapper;
import com.studyweb.mapper.DataAnalysisQuestionMapper;
import com.studyweb.pojo.entity.Paper;
import com.studyweb.pojo.entity.PaperQuestion;
import com.studyweb.pojo.entity.QuestionDetail;
import com.studyweb.pojo.entity.PoliticsQuestion;
import com.studyweb.pojo.entity.CommonSenseQuestion;
import com.studyweb.pojo.entity.LanguageQuestion;
import com.studyweb.pojo.entity.QuantitativeQuestion;
import com.studyweb.pojo.entity.JudgmentQuestion;
import com.studyweb.pojo.entity.DataAnalysisQuestion;
import com.studyweb.service.PaperService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.util.ArrayList;

@Service
public class PaperServiceImpl implements PaperService {
    
    @Autowired
    private PaperMapper paperMapper;
    
    @Autowired
    private PaperQuestionMapper paperQuestionMapper;
    
    // 注入各种题型的Mapper
    @Autowired
    private PoliticsQuestionMapper politicsQuestionMapper;
    
    @Autowired
    private CommonSenseQuestionMapper commonSenseQuestionMapper;
    
    @Autowired
    private LanguageQuestionMapper languageQuestionMapper;
    
    @Autowired
    private QuantitativeQuestionMapper quantityQuestionMapper;
    
    @Autowired
    private JudgmentQuestionMapper judgmentQuestionMapper;
    
    @Autowired
    private DataAnalysisQuestionMapper dataAnalysisQuestionMapper;
    
    @Override
    public Paper createPaper(Paper paper) {
        paper.setCreateTime(LocalDateTime.now());
        paper.setUpdateTime(LocalDateTime.now());
        paperMapper.insert(paper);
        return paper;
    }
    
    @Override
    public Paper getPaperById(Long id) {
        return paperMapper.selectById(id);
    }
    
    @Override
    public List<Paper> getAllPapers() {
        return paperMapper.selectAll();
    }
    
    @Override
    public List<Paper> getPapersByCondition(String name) {
        return paperMapper.selectByCondition(name);
    }
    
    @Override
    public Paper updatePaper(Paper paper) {
        paper.setUpdateTime(LocalDateTime.now());
        paperMapper.update(paper);
        return paper;
    }
    
    @Override
    public void deletePaper(Long id) {
        // 先删除试卷关联的题目
        paperQuestionMapper.deleteByPaperId(id);
        // 再删除试卷
        paperMapper.deleteById(id);
    }
    
    @Override
    @Transactional
    public void addQuestionsToPaper(Long paperId, List<PaperQuestion> paperQuestions) {
        // 添加新的题目关联（不删除原有题目）
        for (int i = 0; i < paperQuestions.size(); i++) {
            PaperQuestion paperQuestion = paperQuestions.get(i);
            paperQuestion.setPaperId(paperId);
            paperQuestion.setQuestionOrder(i + 1);
            paperQuestion.setCreateTime(LocalDateTime.now());
            paperQuestionMapper.insert(paperQuestion);
        }
    }
    
    @Override
    public List<PaperQuestion> getPaperQuestions(Long paperId) {
        return paperQuestionMapper.selectByPaperId(paperId);
    }
    
    @Override
    public Map<String, List<QuestionDetail>> getPaperQuestionsWithDetails(Long paperId) {
        // 获取试卷关联的题目
        List<PaperQuestion> paperQuestions = paperQuestionMapper.selectByPaperId(paperId);
        
        // 按模块分类题目
        Map<String, List<QuestionDetail>> questionsByModule = new HashMap<>();
        questionsByModule.put("politics", new ArrayList<>());
        questionsByModule.put("commonSense", new ArrayList<>());
        questionsByModule.put("language", new ArrayList<>());
        questionsByModule.put("quantity", new ArrayList<>());
        questionsByModule.put("judgment", new ArrayList<>());
        questionsByModule.put("dataAnalysis", new ArrayList<>());
        
        // 根据题目类型获取详细内容
        for (PaperQuestion paperQuestion : paperQuestions) {
            String module = paperQuestion.getQuestionType();
            Long questionId = paperQuestion.getQuestionId();
            
            QuestionDetail questionDetail = null;
            switch (module) {
                case "politics":
                    PoliticsQuestion politicsQuestion = politicsQuestionMapper.selectById(questionId);
                    if (politicsQuestion != null) {
                        questionDetail = convertToQuestionDetail(politicsQuestion);
                    }
                    break;
                case "commonSense":
                    CommonSenseQuestion commonSenseQuestion = commonSenseQuestionMapper.selectById(questionId);
                    if (commonSenseQuestion != null) {
                        questionDetail = convertToQuestionDetail(commonSenseQuestion);
                    }
                    break;
                case "language":
                    LanguageQuestion languageQuestion = languageQuestionMapper.selectById(questionId);
                    if (languageQuestion != null) {
                        questionDetail = convertToQuestionDetail(languageQuestion);
                    }
                    break;
                case "quantity":
                    QuantitativeQuestion quantitativeQuestion = quantityQuestionMapper.selectById(questionId);
                    if (quantitativeQuestion != null) {
                        questionDetail = convertToQuestionDetail(quantitativeQuestion);
                    }
                    break;
                case "judgment":
                    JudgmentQuestion judgmentQuestion = judgmentQuestionMapper.selectById(questionId);
                    if (judgmentQuestion != null) {
                        questionDetail = convertToQuestionDetail(judgmentQuestion);
                    }
                    break;
                case "dataAnalysis":
                    DataAnalysisQuestion dataAnalysisQuestion = dataAnalysisQuestionMapper.selectById(questionId);
                    if (dataAnalysisQuestion != null) {
                        questionDetail = convertToQuestionDetail(dataAnalysisQuestion);
                    }
                    break;
            }
            
            // 设置题目顺序
            if (questionDetail != null) {
                questionDetail.setQuestionOrder(paperQuestion.getQuestionOrder());
            }
            
            if (questionDetail != null && questionsByModule.containsKey(module)) {
                questionsByModule.get(module).add(questionDetail);
            }
        }
        
        return questionsByModule;
    }
    
    /**
     * 将PoliticsQuestion转换为QuestionDetail
     */
    private QuestionDetail convertToQuestionDetail(PoliticsQuestion question) {
        QuestionDetail detail = new QuestionDetail();
        detail.setId(question.getQuestionId());
        detail.setQuestionType(question.getQuestionType());
        detail.setContent(question.getContent());
        detail.setOptions(question.getOptions());
        detail.setCorrectAnswer(question.getCorrectAnswer());
        detail.setExplanation(question.getExplanation());
        detail.setCreateTime(question.getCreateTime());
        detail.setUpdateTime(question.getUpdateTime());
        return detail;
    }
    
    /**
     * 将CommonSenseQuestion转换为QuestionDetail
     */
    private QuestionDetail convertToQuestionDetail(CommonSenseQuestion question) {
        QuestionDetail detail = new QuestionDetail();
        detail.setId(question.getQuestionId());
        detail.setQuestionType(question.getQuestionType());
        detail.setContent(question.getContent());
        detail.setOptions(question.getOptions());
        detail.setCorrectAnswer(question.getCorrectAnswer());
        detail.setExplanation(question.getExplanation());
        detail.setCreateTime(question.getCreateTime());
        detail.setUpdateTime(question.getUpdateTime());
        return detail;
    }
    
    /**
     * 将LanguageQuestion转换为QuestionDetail
     */
    private QuestionDetail convertToQuestionDetail(LanguageQuestion question) {
        QuestionDetail detail = new QuestionDetail();
        detail.setId(question.getQuestionId());
        detail.setQuestionType(question.getQuestionType());
        detail.setContent(question.getContent());
        detail.setOptions(question.getOptions());
        detail.setCorrectAnswer(question.getCorrectAnswer());
        detail.setExplanation(question.getExplanation());
        detail.setCreateTime(question.getCreateTime());
        detail.setUpdateTime(question.getUpdateTime());
        return detail;
    }
    
    /**
     * 将QuantitativeQuestion转换为QuestionDetail
     */
    private QuestionDetail convertToQuestionDetail(QuantitativeQuestion question) {
        QuestionDetail detail = new QuestionDetail();
        detail.setId(question.getQuestionId());
        detail.setQuestionType(question.getQuestionType());
        detail.setContent(question.getContent());
        detail.setOptions(question.getOptions());
        detail.setCorrectAnswer(question.getCorrectAnswer());
        detail.setExplanation(question.getExplanation());
        detail.setCreateTime(question.getCreateTime());
        detail.setUpdateTime(question.getUpdateTime());
        return detail;
    }
    
    /**
     * 将JudgmentQuestion转换为QuestionDetail
     */
    private QuestionDetail convertToQuestionDetail(JudgmentQuestion question) {
        QuestionDetail detail = new QuestionDetail();
        detail.setId(question.getQuestionId());
        detail.setQuestionType(question.getQuestionType());
        detail.setContent(question.getContent());
        detail.setOptions(question.getOptions());
        detail.setCorrectAnswer(question.getCorrectAnswer());
        detail.setExplanation(question.getExplanation());
        detail.setCategory(question.getCategory()); // 设置category字段
        detail.setCreateTime(question.getCreateTime());
        detail.setUpdateTime(question.getUpdateTime());
        return detail;
    }
    
    /**
     * 将DataAnalysisQuestion转换为QuestionDetail
     */
    private QuestionDetail convertToQuestionDetail(DataAnalysisQuestion question) {
        QuestionDetail detail = new QuestionDetail();
        detail.setId(question.getQuestionId());
        detail.setQuestionType(question.getQuestionType());
        detail.setContent(question.getContent());
        detail.setOptions(question.getOptions());
        detail.setCorrectAnswer(question.getCorrectAnswer());
        detail.setExplanation(question.getExplanation());
        detail.setCreateTime(question.getCreateTime());
        detail.setUpdateTime(question.getUpdateTime());
        return detail;
    }
    
    @Override
    public void deletePaperQuestions(Long paperId) {
        paperQuestionMapper.deleteByPaperId(paperId);
    }
    
    @Override
    public void removeQuestionFromPaper(Long paperId, Long questionId) {
        paperQuestionMapper.deleteByPaperIdAndQuestionId(paperId, questionId);
    }
}